<?php
/**
 * @copyright meila.com
 * @author wangtao5@
 * @name 发布接口
 * @param
 *         $nick = 作用
 *
 *
 */
include('../../__global.php');
include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_content.php');
class aj_albumreply_publish extends ml_controller
{
    private $_uid;
    //执行默认检查的参数集合
    protected  $_param_check = array(
            'albumfeed_rid' => array('type'=>ML_DATATYPE_ALNUM, 'is_must'=>true,'len_limit'=>array('eq'=>ML_RID_LEN)),    
            'content'     => array('call'=>'callback', 'is_must'=>true),
    );
    //错误描述
    protected  $_error_msg = array(
            'albumfeed_rid' => 'albumfeed_rid',
            'content' => 'content',
    );
    private $_replay_rid;
    private $_album_id;
    private $_oAlbumfeed;
    private $_count;
    private $_status_array = array('ok'=>1,'del'=>9);
    /**
     * 初始化参数
     */
    function initParam()
    {
        //必选项
        $this->_option['albumfeed_rid'] = Tool_input::input('albumfeed_rid', 'P', '');
        $this->_option['content'] = Tool_input::input('content', 'P', '');
        $this->_uid = $this->__visitor['uid'];
    }
    
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam()
    {
        //检查可写权限
        if(!$this->check_permission(ML_PERMISSION_LOGIN_CANWRITE)) {
            return $this->api_output(ML_RCODE_NOACTIVE);
        }
        $this->check_option();
    }
    
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main(){
        $rs = $this->_check_albumfeed();
        if(!$rs)
            $this->api_output(ML_RCODE_HACK);
        $oGenerator = new ml_model_dbIdGenerator();
        $this->_album_id = ml_tool_resid::resid2uid($this->_option['albumfeed_rid']);
        $replys_rid = 0;
        $rs = $oGenerator->get_albumreply_rid($this->_album_id, &$replys_rid);
        if(!$rs)
            $this->api_output(ML_RCODE_HACK);
        
        $this->_replay_rid = $replys_rid;
        $result = $this->save_data();
        if(!$result)
            $this->api_output(ML_RCODE_HACK);
        
        $this->incr_reply_cnt();
        //$bizAlbumreply = new ml_biz_feed_albumreply();
        //$option['uid'] = $this->_uid;
        //$option['data'] = array($result);
        //$bizAlbumreply->initParam($option);
        //$data['data'] = $bizAlbumreply->create_html(false);
        //$data['count'] = $this->_count;
        $this->_private_action_log();
        $this->api_output(ML_RCODE_SUCC);
    }
    /**
     * @检查$albumfeed_rid是否存在
     */
    private function _check_albumfeed(){
        $this->_oAlbumfeed = new ml_model_album_dbAlbumfeed();
        $rs = $this->_oAlbumfeed->get_by_albumfeed_rid($this->_option['albumfeed_rid']);
        if(!$rs)
            return false;
        $albumfeed = $this->_oAlbumfeed->get_data();
        if(empty($albumfeed))
            return false;
        if($this->_status_array['del'] == $albumfeed['status'])
            return false;
        return true;
    }
    /**
     * 添加行为日志
     */
    private function _private_action_log(){
        $ext = array('albumfeed_id'=>$this->_option['albumfeed_rid']);
        ml_tool_actionlog::add_other_action($this->_uid ,ML_SINAACTCODE_ALBUM_REPLY_PUBLISH, $this->_replay_rid,0,$ext);
    }
    function incr_reply_cnt(){
        $oRdsCount = new ml_model_rdsCount();
        $rds_rs = $oRdsCount->incrAlbumreplyCnt($this->_option['albumfeed_rid']);
        //$this->_count = $oRdsCount->getAlbumreplyCnt($this->_option['albumfeed_rid']);
        
        $oRedisAlbum = new ml_model_album_rdsAlbum();
        $rs = $oRedisAlbum->set_album_users($this->_album_id, $this->_uid);
        $rs = $oRedisAlbum->set_album_replay_users($this->_option['albumfeed_rid'], $this->_uid);
        $rs = $this->_oAlbumfeed->incr_reply_cnt_by_albumfeed_rid($this->_option['albumfeed_rid']);
        return $rs;
    }
    /**
     * @保存数据
     */
    function save_data(){
        $data['albumfeed_rid'] = $this->_option['albumfeed_rid'];
        $data['uid'] = $this->_uid;
        $data['content'] = $this->_option['content'];
        $data['ctime'] = date('Y-m-d H:i:s');
        $data['albumreply_rid'] = $this->_replay_rid;
        $oAlbumreply = new ml_model_album_dbAlbumreply();
        $rs = $oAlbumreply->insert_content($data);
        if(!$rs)
            return false;
        return $data;
    }
    
    /**
     * @检测发布内容参数
     */
    protected  function _param_content(){
        
        $this->_option['content'] = preg_replace('/\s+/',' ', $this->_option['content']);
        
        if(empty($this->_option['content']))
            return false;
        
        $this->_option['content'] = ml_tool_formatContent::repalcelink ($this->_option['content']);
        
        if(Tool_string::str_width($this->_option['content']) > ML_DATARULE_COMMENT_MAXLEN)
            return false;
        if(ml_tool_keyword::haveSensitiveWord($this->_option['content']))
            return false;
        return true;
    }
}

new aj_albumreply_publish();
